package codeMonkeyClock;

import processing.core.*;

/**
 *  CodeMonkeyClock - 
 *  This is the front end.
 *  Copyright (C) 2013  Samantha Lycett
 *  
 *  This file is part of CodeMonkeyClock.
 *
 *  CodeMonkeyClock is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  CodeMonkeyClock is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CodeMonkeyClock.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * CodeMonkeyClock displays the system time + a random amount fast, upto 15 minutes
 * Displays a clock face
 * 
 * @author Samantha Lycett
 * @created 13 June 2013
 * @version 18 June 2013
 */
public class CodeMonkeyClock extends PApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5992221550746931622L;
	
	Clock thisClock 	= Clock.getClock();
	PFont clockFont 	= createFont("Arial",16,true);
	
	float xc;											// centre of clock
	float yc;											// centre of clock
	float minD;											// smallest dimension of canvas
	float circleSize 	= 10;							// size of dots on clock face
	
	
	public void setup() {
		size(200,200);
		frameRate(1);
		
		colorMode(HSB,360,100,100);  // set colour mode to be H,S,B
        // Hue        from 0 - 360
        // Saturation from 0 - 100
        // brightness from 0 - 100
	}
	
	public void draw() {
		
		// set background to white
		background(0,0,100);
		
		// increment the clock
		thisClock.tick();
		
		// find current minimum dimension of canvas
		minD = height;
		if (width < height) minD = width;
		
		// add title and calculate yoffset
		float yoffset = addTitle();
		
		// calculate centre of clock
		xc = (width/2);
		yc = (height/2)+(yoffset/2);
		
		// draw face
		drawFace();
		
		// draw hands
		drawHands();
		
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	private void drawFace() {
		
		float r	 = 60*minD/200;		// 75
		float r2 = 75*minD/200;		// 85
		
		stroke(0,0,25);
		fill(0,0,100);
		ellipse(xc, yc, minD*(float)0.83, minD*(float)0.85);	// 99
				
		// hour positions
		for (int i = 1; i <= 12; i++) {
			double angle= ((double)i/(double)12)*2*Math.PI - (Math.PI/2);
			float xpos = xc + r*(float)Math.cos(angle);
			float ypos = yc + r*(float)Math.sin(angle);
			
			fill( i*360/12, 50, 100);
			ellipse(xpos,ypos,circleSize,circleSize);
			
			
			String ss 	= ""+i;
			float fs 	= Math.round( 12*minD/200 );
			float xpos2 = xc + r2*(float)Math.cos(angle);
			float ypos2 = yc + r2*(float)Math.sin(angle);
			
			textFont(clockFont,fs);                 // font size scales as window size
			textAlign(LEFT);
			text(ss, xpos2-textWidth(ss)/2, ypos2+((float)0.75*fs/2));
			
			//ellipse(xpos2,ypos2,1,1);
			
		}
		
	}
	
	private void drawHands() {
				
		int th   	= thisClock.getMonkeyTime_as_ints()[0];
		int tm   	= thisClock.getMonkeyTime_as_ints()[1];
		int ts   	= thisClock.getMonkeyTime_as_ints()[2];

		// hour hand
		float hh	= (float)(th % 12)/12 + ((float)tm/(12*60));	// fractional
		new Hand(this, (float)0.48, hh, (float)0.1);				// 50
		
		// minute hand
		new Hand(this, (float)0.58, (float)tm/60, (float)0.075);	// 75
		
		// second hand
		new Hand(this, (float)0.58, (float)ts/60, (float)0);		// 75
		
		// centre dot	(draw after hands)
		fill(200,50,75);
		ellipse(xc,yc,circleSize,circleSize);
	}
	
	/**
	 * adds title to clock
	 * returns yposition of title so that centre of clock can be offset
	 * @return ypos
	 */
	private float addTitle() {
		float fs = 14*minD/200;
		textFont(clockFont,fs);                 // font size scales as window size		
		fill(0);                        		// Specify font color (black)
		
		String ss = "Code Monkey Time";
		textAlign(CENTER);
		float xpos = width/2;
		float ypos = fs*(float)1.1;//height*(float)0.05;
		text(ss,xpos,ypos);
		
		// returns yposition of title, so that centre of clock can be offset
		return ypos;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("** CodeMonkeyClock **");
		PApplet.main(new String[] { "--present", "codeMonkeyClock.CodeMonkeyClock" });
		System.out.println("** END **");
		
	}
	
}
